import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import DefineOptions from 'unplugin-vue-define-options/vite'
import path from 'path';
import {fileURLToPath} from 'node:url';
import dts from "vite-plugin-dts";
import postcss from 'rollup-plugin-postcss'
import autoprefixer from "autoprefixer"
import cssnano from "cssnano"
import copy from "rollup-plugin-copy"
import del from "rollup-plugin-delete"
import {terser} from "rollup-plugin-terser";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

// 构建产物
const output = 'output';
// es产物
const es_output = `${output}/es`;
// lib产物
const lib_output = `${output}/lib`;
// dist产物
const dist_output = `${output}/dist`;

const plugins = [
    dts({
        outDir: [es_output, lib_output, `${dist_output}/@types`],
        tsconfigPath: path.resolve(__dirname, "tsconfig.json"),
        compilerOptions: {
            baseUrl: ".",
            paths: {
                "@akong-ui/utils": ["./packages/utils"],
                "@akong-ui/components": ["./packages/components"],
            }
        }
    }),
    postcss({
        extract: 'index.css',
        plugins: [
            autoprefixer(),
            cssnano()
        ],
    }),
    terser({
        format: {
            // 保留至少包含 "@license" 或 "@preserve" 的注释
            comments: 'all'
        },
    }),
    copy({
        targets: [
            {src: `${es_output}/*.css`, dest: 'dist'}
        ],
        verbose: true,
        hook: 'generateBundle'

    }),
    del({
        targets: [
            // 设置删除规则，删除原来位置的 CSS 文件
            `${es_output}/*.css`,
            `${lib_output}/*.css`,
            `${dist_output}/style.css`,
        ],
        hook: 'closeBundle'
    }),
]


export default defineConfig({
    plugins: [
        vue(),
        DefineOptions(),
        // 打包模式下启用
        ...plugins
    ],

    build: {
        rollupOptions: {
            external: ["vue", '@vueuse/core', '@floating-ui/vue'],
            input: '@akong-ui/components/index.ts',
            output: [
                {
                    format: "es",
                    entryFileNames: "[name].mjs",
                    // 让打包目录和我们目录对应
                    preserveModules: true,
                    // 配置打包根目录
                    dir: es_output,
                    inlineDynamicImports: false
                },
                {
                    // 打包格式
                    format: "cjs",
                    // 打包后文件名
                    entryFileNames: "[name].js",
                    // 让打包目录和我们目录对应
                    preserveModules: true,
                    // 配置打包根目录
                    dir: lib_output,
                    inlineDynamicImports: false
                },
            ],
        },
        lib: {
            entry: "@akong-ui/components/index.ts",
            name: 'akong-ui',
            fileName: 'akong-ui'
        }
    },
    css: {
        preprocessorOptions: {
            less: {
                options: {
                    javascriptEnabled: true
                }
            }
        }
    }
})
